<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<script src="./jQuery/jquery-1.8.0.js"></script>
		<script src="util.js"></script>
		<link rel="stylesheet" type="text/css" href="style.css" />
		<title>账号管理</title>
	</head>
	<body id='userMgr'>
		<div id='userMgr'>
			<h1>更新账号信息</h1>
			<table align="center" >
				<tr>
					<td class='caption'>用户名</td>
					<td class='input'><input name="name" type="text" maxlength="20" autocomplete='off' readonly='true' /></td>
				</tr>
				<tr>
					<td class='caption'>昵&nbsp;&nbsp;称</td>
					<td class='input'><input name="nickname" type="text" maxlength="10" autocomplete='off'></input></td>
				</tr>
				<tr>
					<td class='caption'>旧密码</td>
					<td class='input'><input name="oldPwd" type="password" maxlength="18"></input></td>
				</tr>
				<tr>
					<td class='caption'>新密码</td>
					<td class='input'><input name="pwd" type="password" maxlength="18"></input></td>
				</tr>
				<tr>
					<td class='caption'>确认密码</td>
					<td class='input'><input name="pwd2" type="password" maxlength="18"></input></td>
				</tr>
				<tr>
					<td align='center'><button class='button loginBtn' onclick="return check();">更新</button></td>
					<td align='center'><button style='margin-left:50px;' class='button loginBtn' onclick="onBackClick()">返回</button></td>
				</tr>
			</table>
			<label id='notice' style="color:red; margin-top:10px;" hidden></label>
		</div>
		<script type="text/javascript">
			if(getCookie('login') && getCookie('login') == 'true') {
				$('input[name=name]').attr('value', getCookie('username'));
				$('input[name=nickname]').attr('value', getCookie('nickname'));
			} else {
				location.href = 'login.html';
			}
				
			$('input[name=pwd').blur(function() {
				if($(this).attr('value') == '')
					return;
				ret = /^[A-Za-z0-9]+$/g;
				if(!ret.test($(this).val())) {
					$('#notice').html('密码只能是字母和数字组合！！');
					$('#notice').show();
				} else {
					$('#notice').hide();
				}
			});
			function check() {
				var name = $('input[name=name]').val();
				var nickname = $('input[name=nickname]').val();
				var oldPwd = $('input[name=oldPwd]').val();
				var pwd = $('input[name=pwd]').val();
				var pwd2 = $('input[name=pwd2]').val();
				
				if($('#notice').is(':visible'))
					return;
				
				if(nickname == "") {
					alert("请输入昵称");
					$('input[name=nickname]').focus();
					return false;
				}
				if(oldPwd == "") {
					alert("请输入旧密码");
					$('input[name=oldPwd]').focus();
					return false;
				}
				if(pwd == "") {
					alert("请输入新密码");
					$('input[name=pwd]').focus();
					return false;
				}
				if(pwd2 == "") {
					alert("请输入确认密码");
					$('input[name=pwd2]').focus();
					return false;
				}
				if(pwd !== pwd2) {
					alert("两次密码不一致，请重新输入");
					$('input[name=pwd]').focus();
					return false;
				}
				
				$.post('checkLogin.php', 
					{
						type: "updateUser",
						name: name,
						nickname: nickname,
						oldPwd: oldPwd,
						pwd: pwd
					},
					function(data, status) {
						if(status == 'success') {
							sub = (trimStr(data)).split(',');
							if(sub[0] == 'success') {
								alert(name + ' 用户信息更新成功！');
								setCookie('nickname', nickname, 36000);
								location.reload();
							} else if(sub[0] == 'oldPwdError') {
								alert('旧密码输入错误！');
							} else {
								alert('用户信息更新失败！' + sub[1]);
							}
						}
				});
			}
			function onBackClick() {
				history.go(-1);
			}
		</script>
	</body>
</html>